# coding=utf-8
from fastapi import FastAPI, Depends, APIRouter, HTTPException
from sqlalchemy.orm import Session
from 学生管理系统.Dao import Eployment_dao
from 学生管理系统.Mondel.Eployment_model import Eployment
from 学生管理系统.Datebase.Datebase import SessionLocal
from 学生管理系统.Query.Eployment import Eployment1

app03 = APIRouter()



# 依赖注入，每次调用接口都会进入这里实例化会话层
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@app03.get("/students/queryall", summary=["查询所有学生信息"])
def get_query(db: Session = Depends(get_db)):
    # 调用dao层sql语句接口
    return Eployment_dao.get_query(db)



# {student_id}/{student_name}/{student_class_name}获取学生编号，学生姓名，班级名称，精准查询学生信息
@app03.get("/students/{student_id}/{company_name}/{salary}/queryone",summary=["用学生编号，学生姓名，班级名称，多级联查学生信息"])
def query_employment(student_id: int, company_name: str, salary: float, db: Session = Depends(get_db)):
    result = db.query(Eployment).filter(
        Eployment.student_id == student_id,  # 关联学生ID（非就业表id）
        Eployment.company == company_name,
        Eployment.salary == salary
    ).first()
    if not result:
        raise HTTPException(status_code=404, detail='就业信息不存在')
    return result


# 先判断学生是否存在，获取学生编号{student_id}，添加学生接口
@app03.post("/students/{student_id}/add",  summary=["添加就业表信息"])
def get_query(student_id: int, Eployments: Eployment1, db: Session = Depends(get_db)):
    # 调用dao层sql语句接口
    emp = Eployment_dao.get_one_query(db, student_id)
    if emp :
        raise HTTPException(status_code=404, detail='该学生已存在')
    return Eployment_dao.get_job_add(db, Eployments)


# 先判断学生是否存在，获取学生编号{student_id}，修改学生接口
@app03.put("/students/{student_id}/update", summary=["修改就业表信息"])
def get_query(student_id: int, Eployments: Eployment1, db: Session = Depends(get_db)):
    # 调用dao层sql语句接口
    emp2 = Eployment_dao.get_one_query(db, student_id)
    if emp2 is None:
        raise HTTPException(status_code=404, detail='该学生不存在')
    return Eployment_dao.get_job_update(db, Eployments)


# 先判断学生是否存在，获取学生编号{student_id}，删除学生接口
@app03.delete("/students/{student_id}/delete",  summary=["删除就业表信息"])
def get_query(student_id: int, db: Session = Depends(get_db)):
    # 调用dao层sql语句接口
    emp3 = Eployment_dao.get_one_query(db, student_id)
    if emp3 is None:
        raise HTTPException(status_code=404, detail='该学生不存在')
    return Eployment_dao.get_job_delete(db, student_id)
